//
//  FSChengbanController.m
//  OASystem
//
//  Created by 刘沿 on 16/5/26.
//  Copyright © 2016年 zpf. All rights reserved.
//

#import "FSChengbanController.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "HttpTools.h"
#import "CountyDepartmentListModel.h"
#import "HeaderView.h"
#import "OADepartmentModel.h"
#import "OACheckBoxFootView.h"
#import "GDataDefines.h"
#import "Toast+UIView.h"


@interface FSChengbanController ()<HeaderViewDelegate>
@property(nonatomic,strong)NSMutableArray * subArray;
@property(nonatomic,strong)UIBarButtonItem * rightItem;

@end

@implementation FSChengbanController
-(NSMutableArray*)groupArray{
    
    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    
    return _groupArray;
}

-(NSMutableArray*)subArray{
    
    if (_subArray == nil) {
        _subArray = [NSMutableArray array];
    }
    
    return _subArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
            _operationItem.enabled = YES;

    [self setRightItem];
    
//    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
//    
//    NSArray *array = @[@{@"userId":userId}];
//    
//    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
//    
//    [HttpTools requestWithURL:KExchTree arrays:array method:KLocalTree success:^(id JSON) {
//        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
//        NSLog(@"%@",JSON);
//        
//        NSArray * data = JSON[@"data"];
//        if (data.count) {
//            NSDictionary * ditc = [data firstObject];
//            [self.contentsArray addObjectsFromArray:[CountyDepartmentListModel dataWithArray:ditc[@"PartNode"]]];
//            
//            self.groupArray = [self groups];
            
            for (CountyDepartmentListModel * model in self.groupArray) {
                NSMutableArray * subArray = [NSMutableArray array];
                for (CountyDepartmentListModel * listModel in self.contentsArray) {
                    if ([model.depid isEqualToString:listModel.depid] && listModel.bmmc.length ==0) {
                        [subArray addObject:listModel];
                        model.subArrays = subArray;
                    }
                }
            }
//
//            [self.tableView reloadData];
//        }
//        
//    } failure:^(NSError *error) {
//        
//        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
//    }];
    
}


-(void)setRightItem{
    _operationItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(operationACtion)];
    self.navigationItem.rightBarButtonItem = _operationItem;
    self.rightItem = _operationItem;
    
}
//确定提交
-(void)operationACtion{
                _operationItem.enabled = NO;

    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * model in self.personArray){
        if (model.isSelected) {
            [tempArray addObject:model.ryxm];
        }
        
    }
    
    if (tempArray.count == 0) {
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    self.rightItem.enabled = NO;
    NSString * selectPersons = [tempArray componentsJoinedByString:@","];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":selectPersons},@{@"ryorder":@"0"},@{@"famxml":self.body}];
    
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
        
        NSLog(@"%@",JSON);
        
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.rightItem.enabled = YES;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
        
        [self.view makeToast:@"发送成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
        [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
        
        
    } failure:^(NSError *error) {
        self.rightItem.enabled = YES;
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];}

-(void)clickHeaderView:(NSInteger)index{
    CountyDepartmentListModel * listModel = self.groupArray[index];
    listModel.isOpen = !listModel.isOpen;
    [self.tableView reloadData];
}

-(NSMutableArray*)groups{
    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * listModel in self.contentsArray) {
        if (listModel.bmmc.length !=0) {
            [tempArray addObject:listModel];
        }
    }
    
    
    return tempArray;
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CountyDepartmentListModel * listModel = self.groupArray[section];
    return   listModel.isOpen ? listModel.subArrays.count : 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellid = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
    CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    cell.textLabel.text = subModel.ryxm;
    if (subModel.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate = self;
    CountyDepartmentListModel * listModel = self.groupArray[section];
    headView.bgButton.tag = section;
    [headView.bgButton setTitle:listModel.bmmc forState:UIControlStateNormal];
    headView.bgButton.imageView.transform = listModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
    CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
    
    [self.tableView reloadData];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}



@end
